#include "CO2Read.h"

double c;
double cdisplay10;
double ppmd;
double cdisplay;
double ppmdou;
double ppm;

void CO2Read_init(void) {
    Adc_Init();
}
float CO2Read(void) {
    int shiwei=0,ijia=0;
    for(ijia=0; ijia<100; ijia++)
    {
        shiwei=Get_Adc(14);
//	   lcd_pos(0x40);                //????????????1???
//	   lcd_wdat('A');
//	   lcd_pos(0x41);
//	   lcd_wdat('=');
//	   lcd_pos(0x42);
//	   lcd_wdat(shiwei/1000+0x30);
//	   lcd_pos(0x43);
//	   lcd_wdat((shiwei%1000)/100+0x30);
//	   lcd_pos(0x44);
//	   lcd_wdat((shiwei%100)/10+0x30);
//	   lcd_pos(0x45);
//	   lcd_wdat((shiwei%10)+0x30);
        c = ((double)shiwei*(5.00/1024))*1000;
        cdisplay10 += c;

    }
    //for()
    ppmd = cdisplay10/100;
    cdisplay =  (int)(cdisplay10/100);

    cdisplay10 = 0;

    if(cdisplay>1072)
    {
        ppm=400;
    }
    if((1049<cdisplay)&&(cdisplay<=1072))
    {
        ppmdou = (500-((cdisplay-1049)*(100/(1072-1049))));
        ppm = (int)ppmdou;

    }
    if((1036<cdisplay)&&(cdisplay<=1049))
    {
        ppmdou = (600-((cdisplay-1036)*(100/(1049-1036))));
        //ppmdou = (500-(ppmd-1049)*(100/(1072-1049)));
        ppm = (int)ppmdou;

    }

    if((1023<cdisplay)&&(cdisplay<=1036))
    {
        ppmdou = (700-(cdisplay-1023)*(100/(1036-1023)));
        ppm = (int)ppmdou;
    }
    if((1016<cdisplay)&&(cdisplay<=1023))
    {
        ppmdou = (800-((cdisplay-1016)*(100/(1023-1016))));
        ppm = (int)ppmdou;
    }

    if((1006<cdisplay)&&(cdisplay<=1016))
    {
        ppmdou = (900-((cdisplay-1006)*(100/(1016-1006))));
        ppm = (int)ppmdou;
    }
    if((1000<cdisplay)&&(cdisplay<=1006))
    {
        ppmdou = (1000-((cdisplay-1000)*(100/(1016-1000))));
        ppm = (int)ppmdou;
    }
    if((966<cdisplay)&&(cdisplay<=1000))
    {
        ppmdou = (2000-((cdisplay-966)*(1000/(1000-966))));
        ppm = (int)ppmdou;
    }
    if((947<cdisplay)&&(cdisplay<=966))  //2000-3000
    {
        ppmdou = (3000-((cdisplay-947)*(1000/(966-947))));
        ppm = (int)ppmdou;
    }
    if((924<cdisplay)&&(cdisplay<=947))//3000-4000
    {
        ppmdou = (4000-((cdisplay-924)*(1000/(947-924))));
        ppm = (int)ppmdou;
    }
    if((907<cdisplay)&&(cdisplay<=924))//4000-6000
    {
        ppmdou = (6000-(cdisplay-907)*(2000/(924-907)));
        ppm = (int)ppmdou;
    }
    //if(891<cdisplay<=907)//6000-8000
    // {
    //	  ppmdou = (8000-(cdisplay-891)*(2000/(907-891)));
    //	  ppm = (int)ppmdou;
    //}
    //if(874<cdisplay<=1000)//8000-10000
    // {
    //	  ppmdou = (10000-(cdisplay-874)*(2000/(891-874)));
    //	   ppm = (int)ppmdou;
    //}
    if(cdisplay<874)//8000-10000
    {
        ppm = 10000;
    }

    //
    //if(947<cdisplay<=966)  //2000-3000
    ///  {
    // 	  ppmdou = (3000-(cdisplay-947)*(1000/(966-947)));
    //	  ppm = (int)ppmdou;
    // }
    //}
    //  if(924<cdisplay<=947)//3000-4000
    //  {
    //  	  ppmdou = (4000-(cdisplay-924)*(1000/(947-924)));
    //	  ppm = (int)ppmdou;
    // }
    //  if(cdisplay<874)//8000-10000
    // {
    //  	  ppm = 10000;
    //}
    // 	if(874<cdisplay<=1000)//8000-10000
    // {
    //  ppmdou = (10000-((cdisplay-874)*(9000/(1000-874))));
    //   ppm = (int)ppmdou;
    //}

    //if(874<cdisplay<=891)
    // {
    //	 ppmd = (10000-(cdisplay-874)*(2000/(891-874)));
    //	 ppm = (int)ppmd;
    //}
    //ppm =  cdisplay/100;
    //lcd_wdat(0x30);
    return (float)ppm;
}
